﻿using DepartmentWork.Migrator.Data;
using DepartmentWork.Model;
using DepartmentWork.Platform.Controllers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace DepartmentWork.Platform.Areas.QueryArea.Controllers
{
    public class QueryController : BaseController
    {
        //
        // GET: /QueryArea/Query/

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="collection"></param>
        /// <returns></returns>
        public ActionResult Query(FormCollection collection)
        {
            int WordCount = 0;//科室
            int AppopsCount = 0;//运维
            int TaskBookCount = 0;//开发项目任务书
            int BugCount = 0;//开发项目BUG
            int TaskCount = 0;//开发项目任务
            int WorkRecordCount = 0;//项目管理工作记录

            int COUNT = 0;//总数
            string input = collection["selcetAll"];
            #region 设置分页

            var pageSizeExecute = this.Request["pageSizeExecute"] == null ? 10 : int.Parse(Request["pageSizeExecute"]);
            var pageIndexExecute = this.Request["pageIndexExecute"] == null ? 1 : int.Parse(Request["pageIndexExecute"]);
            ViewData["pageIndexExecute"] = pageIndexExecute;
            ViewData["pageSizeExecute"] = pageSizeExecute;

            #endregion

            #region 查询科室
            if (collection["sign"] == "0" && input != "")//科室工作
            {

                List<PROJECT_DESK_WORK> work = new List<PROJECT_DESK_WORK>();
                List<ProjectDeskWorkViewModel> WORK = new List<ProjectDeskWorkViewModel>();

                work = projectdeskworkRepository.GetModel(r => r.DESKWORK_DESCRIBE.Contains(input) || r.DESKWORK_NAME.Contains(input)).ToList();
                WordCount = work.Count;//查询到科室工作符合条件的数量
                for (int i = 0; i < work.Count; i++)
                {
                    ProjectDeskWorkViewModel viewmodel = new ProjectDeskWorkViewModel();
                    viewmodel.DESKWORK_CREATETIME = work[i].DESKWORK_CREATETIME;
                    viewmodel.DESKWORK_DESCRIBE = work[i].DESKWORK_DESCRIBE;
                    viewmodel.DESKWORK_FOUNDER = work[i].DESKWORK_FOUNDER;
                    viewmodel.DESKWORK_ID = work[i].DESKWORK_ID;
                    viewmodel.DESKWORK_NAME = work[i].DESKWORK_NAME;
                    viewmodel.DESKWORK_OVERTIME = work[i].DESKWORK_OVERTIME;
                    viewmodel.DESKWORK_PRINCIPAL = work[i].DESKWORK_PRINCIPAL;
                    viewmodel.DESKWORK_STATUS = work[i].DESKWORK_STATUS;
                    WORK.Add(viewmodel);
                }
                ViewBag.Work = WORK;
                ViewBag.workk = "科室工作";
            }
            #endregion

            #region 查询运维
            if (collection["sign1"] == "1" && input != "")//运维项目
            {
                //string input = collection["selcetAll"];
                List<PROJECT_MAINTENANCE_APPOPS> appops = new List<PROJECT_MAINTENANCE_APPOPS>();
                List<ProjectManintenanceAppopsViewModel> APPOPS = new List<ProjectManintenanceAppopsViewModel>();
                appops = projectMaintenanceAppopsRepository.GetModel(r => r.APPOPS_DESCRIBE.Contains(input) || r.APPOPS_NAME.Contains(input)).ToList();
                AppopsCount = appops.Count;//运维项目符合查询的数量
                for (int i = 0; i < appops.Count; i++)
                {
                    ProjectManintenanceAppopsViewModel viewmodel = new ProjectManintenanceAppopsViewModel();
                    viewmodel.appops_aaa = appops[i].APPOPS_AAA;
                    viewmodel.appops_businesslinkman = appops[i].APPOPS_BUSINESSLINKMAN;
                    viewmodel.appops_businesstype = appops[i].APPOPS_BUSINESSTYPE;
                    viewmodel.appops_department = appops[i].APPOPS_DEPARTMENT;
                    viewmodel.appops_describe = appops[i].APPOPS_DESCRIBE;
                    viewmodel.appops_frequency = appops[i].APPOPS_FREQUENCY;
                    viewmodel.appops_id = appops[i].APPOPS_ID;
                    viewmodel.appops_name = appops[i].APPOPS_NAME;
                    viewmodel.appops_overtime = appops[i].APPOPS_OVERTIME;
                    viewmodel.appops_phone = appops[i].APPOPS_PHONE;
                    //viewmodel.appops_principal = appops[i].APPOPS_PRINCIPAL;
                    viewmodel.appops_quantity = appops[i].APPOPS_QUANTITY;
                    viewmodel.appops_rank = appops[i].APPOPS_RANK;
                    viewmodel.appops_relevancename = appops[i].APPOPS_RELEVANCENAME;
                    viewmodel.appops_relevanceway = appops[i].APPOPS_RELEVANCEWAY;
                    viewmodel.appops_state = appops[i].APPOPS_STATE;
                    viewmodel.appops_time = appops[i].APPOPS_TIME;
                    viewmodel.appops_workrank = appops[i].APPOPS_WORKRANK;
                    APPOPS.Add(viewmodel);
                }
                ViewBag.Record = APPOPS;
                ViewBag.Appops = "运维项目";
            }

            #endregion

            #region 查询 开发项目任务书
            if (collection["sign2"] == "2" && input != "")//查询开发项目任务书
            {
                //string input = collection["selcetAll"];
                List<PROJECT_DEVELOP_TASKBOOK> taskbook = new List<PROJECT_DEVELOP_TASKBOOK>();
                List<ProjectTaskBookViewModel> TASKBOOK = new List<ProjectTaskBookViewModel>();
                taskbook = projectDevelopTaskBookRepository.GetModel(r => r.TASKBOOK_CONTEXT.Contains(input) || r.TASKBOOK_NAME.Contains(input)).ToList();
                TaskBookCount = taskbook.Count;
                for (int i = 0; i < taskbook.Count; i++)
                {
                    Guid id = taskbook[i].CREATER;
                    var username = userRepository.Find(r => r.USER_ID == id).DISPLAY_NAME.ToString();//要权限
                    ProjectTaskBookViewModel viewmodel = new ProjectTaskBookViewModel();
                    viewmodel.TaskBookContext = taskbook[i].TASKBOOK_CONTEXT;
                    viewmodel.TaskBookCreateTime = taskbook[i].CREATE_TIME.ToString(); ;
                    viewmodel.TaskBookName = taskbook[i].TASKBOOK_NAME;
                    viewmodel.TaskBookId = taskbook[i].TASKBOOK_ID;
                    viewmodel.Creater = username;
                    TASKBOOK.Add(viewmodel);
                }
                ViewBag.taskbook = TASKBOOK;
                ViewBag.TaskBookk = "开发项目任务书";
            }
            #endregion

            #region 查询 开发项目BUG

            if (collection["sign3"] == "3" && input != "")//开发项目BUG
            {
                //string input = collection["selcetAll"];
                List<PROJECT_DEVELOP_BUG> bug = new List<PROJECT_DEVELOP_BUG>();
                List<ProjectBugViewModel> BUG = new List<ProjectBugViewModel>();
                bug = projectDevelopBugRepository.GetModel(r => r.BUG_NAME.Contains(input) || r.DESCRIPTION.Contains(input)).ToList();
                BugCount = bug.Count;
                for (int i = 0; i < bug.Count; i++)
                {
                    Guid id = bug[i].HANDTO_USER;
                    var username = userRepository.GetModel(r => r.USER_ID == id).FirstOrDefault().DISPLAY_NAME;
                    Guid ID = bug[i].CREATER;
                    var handtouser = userRepository.GetModel(r => r.USER_ID == ID).FirstOrDefault().DISPLAY_NAME;
                    ProjectBugViewModel viewmodel = new ProjectBugViewModel();
                    viewmodel.BugContext = bug[i].DESCRIPTION;
                    viewmodel.BugCreateTime = bug[i].CREATETIME.ToString();
                    viewmodel.BugDeadTime = bug[i].DEADTIME.ToString();
                    viewmodel.BugName = bug[i].BUG_NAME;
                    viewmodel.HandToUser = username;
                    viewmodel.Level = bug[i].BUG_LEVEL;
                    viewmodel.Status = bug[i].CHECK_FLAG;
                    viewmodel.BugId = bug[i].BUG_ID;
                    viewmodel.CreaterId = handtouser;
                    BUG.Add(viewmodel);
                }
                ViewBag.Bug = BUG;
                ViewBag.bugg = "开发项目BUG";
            }


            #endregion


            #region 查询 开发项目任务

            if (collection["sign4"] == "4" && input != "")//开发项目BUG
            {
                //string input = collection["selcetAll"];
                List<PROJECT_DEVELOP_TASK> Task = new List<PROJECT_DEVELOP_TASK>();
                List<ProjectTaskViewModel> TASK = new List<ProjectTaskViewModel>();
                Task = projectDevelopTaskRepository.GetModel(r => r.TASK_NAME.Contains(input) || r.TASK_CONTEXT.Contains(input)).ToList();
                TaskCount = Task.Count;
                for (int i = 0; i < Task.Count; i++)
                {
                    Guid id = Task[i].CREATER;
                    var username = userRepository.GetModel(r => r.USER_ID == id).FirstOrDefault().DISPLAY_NAME;
                    ProjectTaskViewModel viewmodel = new ProjectTaskViewModel();
                    viewmodel.TaskName = Task[i].TASK_NAME;
                    viewmodel.TaskContext = Task[i].TASK_CONTEXT;
                    viewmodel.TaskCreateTime = Task[i].CREATE_TIME.ToString();
                    viewmodel.TaskDeadTime = Task[i].DEADTIME.ToString();
                    viewmodel.TaskId = Task[i].TASK_ID;
                    viewmodel.Status = Task[i].CHECK_FLAG;
                    viewmodel.Level = Task[i].TASK_LEVEL;
                    viewmodel.Hour = Task[i].HOUR;
                    viewmodel.Creater = username;
                    TASK.Add(viewmodel);
                }
                ViewBag.Task = TASK;
                ViewBag.Taskk = "开发项目任务";
            }

            #endregion

            #region 查询 项目管理工作记录

            if (collection["sign5"] == "5" && input != "")//开发项目BUG
            {
                //string input = collection["selcetAll"];
                List<PROJECT_WORKRECORD> WorkRecord = new List<PROJECT_WORKRECORD>();
                List<ProjectWorkReocredInfoViewModel> WORKRECORD = new List<ProjectWorkReocredInfoViewModel>();
                WorkRecord = projectWorkReocredRepository.GetModel(r => r.PROJECT_WORKRECORD_CONTENT.Contains(input) || r.PROJECT_WORKRECORD_TITLE.Contains(input)).ToList();
                WorkRecordCount = WorkRecord.Count;
                for (int i = 0; i < WorkRecord.Count; i++)
                {
                    Guid id = WorkRecord[i].PROJECT_WORKRECORD_CREATER;
                    var username = userRepository.GetModel(r => r.USER_ID == id).FirstOrDefault().DISPLAY_NAME;
                    ProjectWorkReocredInfoViewModel viewmodel = new ProjectWorkReocredInfoViewModel();
                    viewmodel.PROJECT_MANAGEMENT_ID = WorkRecord[i].PROJECT_MANAGEMENT_ID;
                    viewmodel.PROJECT_WORKRECORD_CONTENT = WorkRecord[i].PROJECT_WORKRECORD_CONTENT;
                    viewmodel.PROJECT_WORKRECORD_CREATER = WorkRecord[i].PROJECT_WORKRECORD_CREATER;
                    viewmodel.PROJECT_WORKRECORD_CREATETIME = WorkRecord[i].PROJECT_WORKRECORD_CREATETIME;
                    viewmodel.PROJECT_WORKRECORD_ID = WorkRecord[i].PROJECT_WORKRECORD_ID;
                    viewmodel.PROJECT_WORKRECORD_TITLE = WorkRecord[i].PROJECT_WORKRECORD_TITLE;
                    viewmodel.PROJECT_WORKRECORD_CREATERNAME = username;
                    WORKRECORD.Add(viewmodel);
                }
                ViewBag.WorkRecord = WORKRECORD;
                ViewBag.WorkRecordd = "开发项目任务";
            }



            #endregion


            COUNT = WordCount + AppopsCount + TaskBookCount + BugCount + TaskCount + WorkRecordCount;
            ViewData["totalExecute"] = COUNT;
            return View();
        }





        /// <summary>
        /// 科室工作
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        public JsonResult WorkSeclet(Guid roleId)
        {
            ProjectDeskWorkViewModel model = new ProjectDeskWorkViewModel();
            PROJECT_DESK_WORK role = projectdeskworkRepository.GetModel(r => r.DESKWORK_ID == roleId).FirstOrDefault();
            model.DESKWORK_NAME = role.DESKWORK_NAME;
            model.DESKWORK_FOUNDER = role.DESKWORK_FOUNDER;
            model.DESKWORK_PRINCIPAL = role.DESKWORK_PRINCIPAL;
            model.DESKWORK_DESCRIBE = role.DESKWORK_DESCRIBE;
            model.DESKWORK_CREATETIME = role.DESKWORK_CREATETIME;
            if (role.DESKWORK_STATUS == "0")
            {
                model.DESKWORK_STATUS = "未完成";
            }
            else
            {
                model.DESKWORK_STATUS = "完成";
            }
            return Json(model);
        }


        /// <summary>
        /// 运维工作
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        public JsonResult AppopsSeclet(Guid roleId)
        {
            ProjectManintenanceAppopsViewModel model = new ProjectManintenanceAppopsViewModel();
            List<PROJECT_MAINTENANCE_APPOPS_USER> user = projectMaintenanceAppopsUserRepository.GetModel(r => r.APPOPS_APPOPS_ID == roleId).ToList();
            foreach (PROJECT_MAINTENANCE_APPOPS_USER x in user)//遍历关联人员信息
            {
                model.appops_founder += userRepository.GetModel(r => r.USER_ID == x.APPOPS_PRINCIPAL_USER_ID).FirstOrDefault().DISPLAY_NAME + " ";//将名字叠加赋值给model中的负责人
            }
            PROJECT_MAINTENANCE_APPOPS role = projectMaintenanceAppopsRepository.GetModel(r => r.APPOPS_ID == roleId).FirstOrDefault();
            model.appops_businesslinkman = role.APPOPS_BUSINESSLINKMAN;
            model.appops_businesstype = role.APPOPS_BUSINESSTYPE;
            model.appops_department = role.APPOPS_DEPARTMENT;
            model.appops_describe = role.APPOPS_DESCRIBE;
            model.appops_frequency = role.APPOPS_FREQUENCY;
            model.appops_name = role.APPOPS_NAME;
            model.appops_phone = role.APPOPS_PHONE;
            //model.appops_principal = role.APPOPS_PRINCIPAL;
            model.appops_quantity = role.APPOPS_QUANTITY;
            model.appops_rank = role.APPOPS_RANK;
            model.appops_relevancename = role.APPOPS_RELEVANCENAME;
            model.appops_relevanceway = role.APPOPS_RELEVANCEWAY;
            model.appops_workrank = role.APPOPS_WORKRANK;
            model.appops_state = role.APPOPS_STATE;
            return Json(model);
        }



        /// <summary>
        /// 开发项目任务书
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        public JsonResult TaskBookSeclet(Guid roleId)
        {
            ProjectTaskBookViewModel model = new ProjectTaskBookViewModel();
            PROJECT_DEVELOP_TASKBOOK role = projectDevelopTaskBookRepository.GetModel(r => r.TASKBOOK_ID == roleId).FirstOrDefault();
            Guid id = role.CREATER;
            var username = userRepository.Find(r => r.USER_ID == id).DISPLAY_NAME.ToString();//要权限
            model.Creater = username;
            model.TaskBookContext = role.TASKBOOK_CONTEXT;
            model.TaskBookCreateTime = role.CREATE_TIME.ToString("yyyy-MM-dd");
            model.TaskBookName = role.TASKBOOK_NAME;
            return Json(model);
        }




        /// <summary>
        /// 开发项目BUG
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        public JsonResult BugSeclet(Guid bugId)
        {
            ProjectBugViewModel model = new ProjectBugViewModel();
            PROJECT_DEVELOP_BUG role = projectDevelopBugRepository.GetModel(r => r.BUG_ID == bugId).FirstOrDefault();
            Guid id = role.CREATER;
            var username = userRepository.Find(r => r.USER_ID == id).DISPLAY_NAME.ToString();//要权限
            Guid idd = role.HANDTO_USER;
            var handtouser = userRepository.Find(r => r.USER_ID == idd).DISPLAY_NAME.ToString();//要权限
            model.Creater = username;
            model.BugContext = role.DESCRIPTION;
            model.BugCreateTime = role.CREATETIME.ToString("yyyy-MM-dd");
            model.BugName = role.BUG_NAME;
            model.Level = role.BUG_LEVEL;
            model.HandToUser = handtouser;
            model.Status = role.CHECK_FLAG;
            model.BugDeadTime = role.DEADTIME.ToString("yyyy-MM-dd");

            return Json(model);
        }




        /// <summary>
        /// 开发项目任务
        /// </summary>
        /// <param name="taskId"></param>
        /// <returns></returns>
        public JsonResult TaskSeclet(Guid taskId)
        {

            PROJECT_DEVELOP_TASK task = projectDevelopTaskRepository.GetModel(r => r.TASK_ID == taskId).FirstOrDefault();
            FILEINFO file = fileRepository.GetModel(r => r.FILE_TYPE == "DEVELOP_TASK" && r.CONTACT_ID == taskId).FirstOrDefault();

            Guid userId = task.CREATER;
            string showName = userRepository.GetModel(r => r.USER_ID == userId).FirstOrDefault().DISPLAY_NAME;
            Guid HandToUserGuid = task.HANDTO_USER;
            ProjectTaskViewModel model = new ProjectTaskViewModel();
            model.Creater = showName;
            model.Level = task.TASK_LEVEL;
            model.TaskDeadTime = task.DEADTIME.ToString("yyyy-MM-dd");
            model.TaskContext = task.TASK_CONTEXT;
            model.TaskCreateTime = task.CREATE_TIME.ToString("yyyy-MM-dd");
            model.TaskName = task.TASK_NAME;
            model.Hour = task.HOUR;
            model.HandToUser = userRepository.GetModel(r => r.USER_ID == HandToUserGuid).FirstOrDefault().DISPLAY_NAME;
            //如果审核标志不为 2 ==>表明任务尚未完成
            if (task.CHECK_FLAG != 2)
            {
                //如果审核时间大于结束时间 ==>表明任务超时
                if (task.CHECK_TIME != null && DateTime.Compare((DateTime)task.CHECK_TIME, task.DEADTIME) > 0)
                {
                    model.Status = 3;//任务超时完成
                }
                else
                {
                    model.Status = 2;//任务正常完成
                }
            }
            else
            {
                //如果审核时间大于结束时间 ==>表明任务超时
                if (task.CHECK_TIME != null && DateTime.Compare((DateTime)task.CHECK_TIME, task.DEADTIME) > 0)
                {
                    model.Status = 1;//任务超时并且没有完成
                }
                else
                {
                    if (DateTime.Compare(DateTime.Now, task.DEADTIME) > 0)
                    {
                        model.Status = 1;//任务超时并且没有完成
                    }
                    else
                    {
                        model.Status = 0;//任务进行中
                    }
                }
            }

            return this.Json(model);

        }


        /// <summary>
        /// 项目管理工作记录
        /// </summary>
        /// <param name="workReocredId"></param>
        /// <returns></returns>
        public JsonResult WorkReocredSelect(Guid workReocredId)
        {
            PROJECT_WORKRECORD pro = projectWorkReocredRepository.GetModel(r => r.PROJECT_WORKRECORD_ID == workReocredId).FirstOrDefault();
            ProjectWorkReocredInfoViewModel model = new ProjectWorkReocredInfoViewModel();
            model.PROJECT_MANAGEMENT_ID = pro.PROJECT_MANAGEMENT_ID;
            model.PROJECT_WORKRECORD_CONTENT = pro.PROJECT_WORKRECORD_CONTENT;
            model.PROJECT_WORKRECORD_CREATER = pro.PROJECT_WORKRECORD_CREATER;
            Guid userId = model.PROJECT_WORKRECORD_CREATER;
            model.PROJECT_WORKRECORD_CREATERNAME = userRepository.GetModel(r => r.USER_ID == userId).FirstOrDefault().DISPLAY_NAME;
            model.PROJECT_WORKRECORD_CREATETIME = pro.PROJECT_WORKRECORD_CREATETIME;
            model.PROJECT_WORKRECORD_ID = pro.PROJECT_WORKRECORD_ID;
            model.PROJECT_WORKRECORD_TITLE = pro.PROJECT_WORKRECORD_TITLE;
            model.LEVEL = pro.LEVEL;
            return this.Json(model);
        }

    }
}
